var Product = {
    scrollTo : function (scrollTo, time) {
        var scrollFrom = parseInt(document.body.scrollTop, 10),
            i = 0,
            runEvery = 5; // run every 5ms

        scrollTo = parseInt(scrollTo, 10);
        time /= runEvery;

        var interval = setInterval(function () {
            i++;

            document.body.scrollTop = (scrollTo - scrollFrom) / time * i + scrollFrom;

            if (i >= time) {
                clearInterval(interval);
            }
        }, runEvery);
    },
    topSlider : function () {
        var slider = Swipe(document.getElementById('slider'), {
             auto: 3000,
            continuous: true,
            callback: function(pos) {

              var i = bullets.length;
              
              if( i == 2 ){
                if(pos == 1||pos == 3){
                    bullets[0].className = ' ';
                    bullets[1].className = 'on';
                }else{
                    bullets[1].className = ' ';
                    bullets[0].className = 'on';
                }
              }else{
                while (i--) {
                    bullets[i].className = ' ';
                }
                bullets[pos].className = 'on';
              }
            }
          });
        if(document.getElementById('slider-position')){
            var bullets = document.getElementById('slider-position').getElementsByTagName('li');
        }
    },
    topScroll: function () {
        // window.onscroll = function(){
        //     var _scrollTop = $('body').scrollTop();
        //     $('header').css({top: _scrollTop*0.3});
        // };
        document.addEventListener("touchmove", parallaxScroll, false);
        document.addEventListener("scroll", parallaxScroll, false);
        function parallaxScroll () {
            var _scrollTop = $('body').scrollTop();
            $('header').css({top: _scrollTop*0.3});
        }
        // $(window).swipeUp(parallaxScroll);
    },
    initSchedulesNav: function(){
        
        var nav = $('#schedules-nav');
        if(nav.length == 0){
            return;
        }
        var navList = nav.find('li'),
            navContent = $('.schedules'),
            contentBox = navContent.find('.schedules-main li'),
            contentBoxTopMap = {},
            isclicked = false;
        
        
        //点击滚动
        navList.on('click',function(e){
            isclicked = true;
            var index = $.inArray(this,navList.toArray()),
                top = contentBox.eq(index).offset().top-15;
            navList.filter('.cur').removeClass('cur');
            $(this).addClass('cur');
            Product.scrollTo(top-1, 100);
            isclicked = false;
        });
        function schedulesScrollNav () {
            var scrollTop = $(window).scrollTop(),
                navContentTop = navContent.offset().top,
                navContentHeight = navContent.height(),
                navHeight = nav.height();
            //吸顶

            if ( !navigator.userAgent.match(/(iPhone|iPod|iPad)/) ) {
                if( (scrollTop > navContentTop - 1) && (scrollTop < navContentTop + navContentHeight - navHeight - 1) ){
                    nav.addClass('schedules-nav-fixed');
                }else{
                    nav.removeClass('schedules-nav-fixed');
                }
            }
            if( isclicked ){
                return;
            }
            contentBox.each(function(index,item){
                contentBoxTopMap[index] = $(item).offset().top-52;
            });
            //tab选中状态
            for(var i=0;i<navList.length;i++){
                if(i == 0){
                    //页面位于第一个上方
                    if(scrollTop < contentBoxTopMap[0] && !navList.eq(0).hasClass('cur')){
                        navList.filter('.cur').removeClass('cur');
                        navList.eq(0).addClass('cur');
                        return;
                    }
                }else{
                    //页面位于两个模块之间
                    var beforeBoxHeight = contentBox.eq(i-1).height();
                    if(scrollTop > contentBoxTopMap[i-1] + beforeBoxHeight  && scrollTop < contentBoxTopMap[i] && !navList.eq(i).hasClass('cur')){
                        navList.filter('.cur').removeClass('cur');
                        navList.eq(i).addClass('cur');
                        return;
                    }
                }
            }
        }
        $(window).on('scroll', function() {
            schedulesScrollNav();
        });
    }
};

$(function(){
    // Product Page Func
    Product.topSlider();
    
    // Product.topScroll();
    Product.initSchedulesNav();

});